﻿@using eLoan.Models;
@model BorrowerDetailsModel

@{
    ViewBag.Title = "Borrower Details";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Borrower Details</h2>
        <ol class="breadcrumb">
            <li>
                Masterfile
            </li>
            <li>
                <a href="@Url.Action("Borrowers", "Masterfile")">Address Book</a>
            </li>
            <li class="active">
                <strong>Borrower Details</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>
@using (Html.BeginForm("SaveBorrowerDetails", "Borrower", FormMethod.Post, new { id = "saveForm" }))
{
<fieldset>

@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="ibox float-e-margins">
                <div class="col-lg-12">
                    <div class="ibox-title">
                        <h3>@Model.BorrowerName</h3>
                        @Html.HiddenFor(m => Model.BorrowerName)
                        @Html.HiddenFor(m => Model.BranchCode)
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="ibox-title">
                        <h3>Identification</h3>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="ibox-title">
                        <h3>Collaterals</h3>
                    </div>
                </div>
                
                <div class="col-lg-6">
                    <div class="ibox-content contentFixHeight">
                        <div class="scrollable">
                            <table class="table table-bordered">
                                <thead class="header">
                                    <tr>
                                        <th>ID Type</th>
                                        <th>ID No.</th>
                                        <th>Remarks</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (var i = 0; i < Model.BorrowerIdentifications.Count(); i++)
                                    {
                                        <tr>
                                            <td>
                                                @Html.HiddenFor(m => Model.BorrowerIdentifications[i].ID)
                                                @Html.HiddenFor(m => Model.BorrowerIdentifications[i].BorrowerID)
                                                @Html.TextBoxFor(m => Model.BorrowerIdentifications[i].IDType, null, new { @id = "txtIDType_" + i, @class = "text-box single-line", @onchange = "ValidateID(" + i + ");" })
                                                <div id="valIDType_@i" style="display:none;">ID type field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerIdentifications[i].IDNo, null, new { @id = "txtIDNo_" + i, @class = "text-box single-line", @onchange = "ValidateID(" + i + ");" })
                                                <div id="valIDNo_@i" style="display:none;">ID no. field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerIdentifications[i].Remarks, null, new { @id = "txtIDRemarks_" + i, @class = "text-box single-line", @onchange = "ValidateID(" + i + ");" })
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                
                

                <div class="col-lg-6">
                    <div class="ibox-content contentFixHeight">
                        <div class="scrollable">
                            <table class="table table-bordered">
                                <thead class="header">
                                    <tr>
                                        <th>Make / Type</th>
                                        <th>Brand / File No.</th>
                                        <th>Serial / Plate No.</th>
                                        <th>Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (var i = 0; i < Model.BorrowerCollaterals.Count(); i++)
                                    { 
                                    <tr>
                                        <td>
                                            @Html.HiddenFor(m => Model.BorrowerCollaterals[i].ID)
                                            @Html.HiddenFor(m => Model.BorrowerCollaterals[i].BorrowerID)
                                            @Html.TextBoxFor(m => Model.BorrowerCollaterals[i].MakeOrType, null, new { @id = "txtMakeOrType_" + i, @class = "text-box single-line", @onchange = "ValidateCollateral(" + i + ");" })
                                            <div id="valMakeOrType_@i" style="display:none;">Make / Type field is required</div>
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => Model.BorrowerCollaterals[i].BrandOrFileNo, null, new { @id = "txtBrandOrFileNo_" + i, @class = "text-box single-line", @onchange = "ValidateCollateral(" + i + ");" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => Model.BorrowerCollaterals[i].SerialOrPlateNo, null, new { @id = "txtSerialOrPlateNo_" + i, @class = "text-box single-line", @onchange = "ValidateCollateral(" + i + ");" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => Model.BorrowerCollaterals[i].Amount, "{0:#,###,##0.00}", new { @id = "txtAmount_" + i, @class = "text-box single-line", @onchange = "ValidateCollateral(" + i + "); FormatMe(this);" })
                                        </td>
                                    </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="col-lg-6">
                    <div class="ibox-title">
                        <h3>Co-Makers</h3>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="ibox-title">
                        <h3>References</h3>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="ibox-content contentFixHeight">
                        <div class="scrollable">
                            <table class="table table-bordered">
                                <thead class="header">
                                    <tr>
                                        <th>Name</th>
                                        <th>Address</th>
                                        <th>Contact Numbers</th>
                                        <th>ID No</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (var i = 0; i < Model.BorrowerCoMakers.Count(); i++)
                                    {
                                        <tr>
                                            <td>
                                                @Html.HiddenFor(m => Model.BorrowerCoMakers[i].ID)
                                                @Html.HiddenFor(m => Model.BorrowerCoMakers[i].BorrowerID)
                                                @Html.TextBoxFor(m => Model.BorrowerCoMakers[i].CoMakerName, null, new { @id = "txtCoMakerName_" + i, @class = "text-box single-line", @onchange = "ValidateCoMaker(" + i + ");" })
                                                <div id="valCoMakerName_@i" style="display:none;">Name field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerCoMakers[i].Address, null, new { @id = "txtCoMakerAddress_" + i, @class = "text-box single-line", @onchange = "ValidateCoMaker(" + i + ");" })
                                                <div id="valCoMakerAddress_@i" style="display:none;">Address field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerCoMakers[i].ContactNumbers, null, new { @id = "txtCoMakerContactNumbers_" + i, @class = "text-box single-line", @onchange = "ValidateCoMaker(" + i + ");" })
                                                <div id="valCoMakerContactNumbers_@i" style="display:none;">Contact numbers field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerCoMakers[i].IDNo, null, new { @id = "txtCoMakerEmailAddresses_" + i, @class = "text-box single-line", @onchange = "ValidateCoMaker(" + i + ");" })
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                
                <div class="col-lg-6">
                    <div class="ibox-content contentFixHeight">
                        <div class="scrollable">
                            <table class="table table-bordered">
                                <thead class="header">
                                    <tr>
                                        <th>Name</th>
                                        <th>Relationship</th>
                                        <th>Contact Numbers</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (var i = 0; i < Model.BorrowerReferences.Count(); i++)
                                    {
                                        <tr>
                                            <td>
                                                @Html.HiddenFor(m => Model.BorrowerReferences[i].ID)
                                                @Html.HiddenFor(m => Model.BorrowerReferences[i].BorrowerID)
                                                @Html.TextBoxFor(m => Model.BorrowerReferences[i].Name, null, new { @id = "txtReferenceName_" + i, @class = "text-box single-line", @onchange = "ValidateReference(" + i + ");" })
                                                <div id="valReferenceName_@i" style="display:none">Name field is required</div>
                                            </td> 
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerReferences[i].Relationship, null, new { @id = "txtReferenceRelationship_" + i, @class = "text-box single-line", @onchange = "ValidateReference(" + i + ");" })
                                                <div id="valReferenceRelationship_@i" style="display:none">Relationship field is required</div>
                                            </td>
                                            <td>
                                                @Html.TextBoxFor(m => Model.BorrowerReferences[i].ContactNumbers, null, new { @id = "txtReferenceContactNumbers_" + i, @class = "text-box single-line", @onchange = "ValidateReference(" + i + ");" })
                                                <div id="valReferenceContactNumbers_@i" style="display:none">Contact numbers field is required</div>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="col-lg-12">
                    <div class="ibox-footer">
                        @if (Model.IsAdmin)
                        {
                            <button type="submit" id="btnSave" class="btn btn-primary" name="command" value='Save'>Save</button>
                            <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>
                        }
                        else
                        {
                            <button type="submit" id="btnCancel" class="btn btn-primary cancel" name="command" value="Cancel">Cancel</button>
                        }
                    </div>
                </div>
                


            </div>
        </div>
    </div>
</fieldset>
}

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")

    <style>
        .scrollable {
            height: 100%;
            overflow-y: scroll;
            border: solid 1px #EBEBEB;
        }

        .contentFixHeight {
            height: 430px;
            padding-left: 0px; 
            padding-right: 0px;
        }
    </style>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(document).ready(function () {

            GetAgents();

            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    event.preventDefault();
                    return false;
                }
            });
            
        });

        $(function () {
            $("#ddlBranches").change(function () {
                GetAgents()
            });
        });

        function GetAgents()
        {
            var val = $('#ddlBranches').val();
            var subItems = "";

            $.getJSON("@Url.Action("GetAgents","Borrower")", { branchCode: val },
                function (data) {

                    $.each(data, function (index, item) {

                        subItems += "<option value='" + $.trim(item.Value) + "'>" + $.trim(item.Text) + "</option>"

                    });

                    $("#ddlAgents").html(subItems)
                });
        }

    </script>

    <script id="Validations" type="text/javascript">

        function FormatMe(me) {
            var val = $(me).val().replace(',', '').replace(',', '').replace(',', '');

            if (isNaN(parseFloat(val)))
                val = 0.00;

            var decimalFormat = parseFloat(val).toFixed(2);
            var commaFormat = FormatNumber(decimalFormat);

            $(me).val(commaFormat);
        }

        function FormatNumber(nStr) {
            nStr += '';
            var x = nStr.split('.');
            var x1 = x[0];
            var x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }

        function DisableSave(bln) {
            
            if (bln) {
                $('#btnSave').attr('disabled', 'disabled');
            }
            else {
                $('#btnSave').removeAttr('disabled');
            }
            
        }

        function ValidateCoMaker(index)
        {
            var name = $.trim($('#txtCoMakerName_' + index).val());
            var address = $.trim($('#txtCoMakerAddress_' + index).val());
            var contact = $.trim($('#txtCoMakerContactNumbers_' + index).val());
            var email = $.trim($('#txtCoMakerEmailAddresses_' + index).val());

            var valCoMakerName = $('#valCoMakerName_' + index);
            var valCoMakerAddress = $('#valCoMakerAddress_' + index);
            var valCoMakerContactNumbers = $('#valCoMakerContactNumbers_' + index);

            var nameIsBlank = name == '';
            var addressIsBlank = address == '';
            var contactIsBlank = contact == '';
            var emailIsBlank = email == '';

            if (!nameIsBlank && !addressIsBlank && !contactIsBlank && !emailIsBlank) {

                valCoMakerName.hide();
                valCoMakerAddress.hide();
                valCoMakerContactNumbers.hide();

                DisableSave(false);
            }
            else {

                if (nameIsBlank && (!addressIsBlank || !contactIsBlank || !emailIsBlank)) {
                    valCoMakerName.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valCoMakerName.hide();
                    DisableSave(false);
                }

                if (addressIsBlank && (!nameIsBlank || !contactIsBlank || !emailIsBlank)) {
                    valCoMakerAddress.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valCoMakerAddress.hide();
                    DisableSave(false);
                }

                if (contactIsBlank && (!nameIsBlank || !addressIsBlank || !emailIsBlank)) {
                    valCoMakerContactNumbers.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valCoMakerContactNumbers.hide();
                    DisableSave(false);
                }
            }

        }

        function ValidateCollateral(index)
        {
            var type = $.trim($('#txtMakeOrType_' + index).val());
            var brand = $.trim($('#txtBrandOrFileNo_' + index).val());
            var serial = $.trim($('#txtSerialOrPlateNo_' + index).val());
            var amount = $.trim($('#txtAmount_' + index).val());

            var valMakeOrType = $('#valMakeOrType_' + index);

            var typeIsBlank = type == '';
            var brandIsBlank = brand == '';
            var serialIsBlank = serial == '';
            var amountIsZero = parseFloat(amount) == 0 || isNaN(parseFloat(amount));


            if (!typeIsBlank) {

                valMakeOrType.hide();
                DisableSave(false);
            }
            else {

                if (typeIsBlank && (!brandIsBlank || !serialIsBlank || !amountIsZero)) {
                    valMakeOrType.show();
                    DisableSave(true);
                }
                else {
                    valMakeOrType.hide();
                    DisableSave(false);
                }
            }
        }

        function ValidateID(index)
        {
            var type = $.trim($('#txtIDType_' + index).val());
            var no = $.trim($('#txtIDNo_' + index).val());
            var remarks = $.trim($('#txtIDRemarks_' + index).val());

            var valType = $('#valIDType_' + index);
            var valNo = $('#valIDNo_' + index);

            var typeIsBlank = type == '';
            var noIsBlank = no == '';
            var remarksIsBlank = remarks == '';

            if (!typeIsBlank && !noIsBlank) {
                valType.hide();
                valNo.hide();

                DisableSave(false);
            }
            else {
                
                if (typeIsBlank && (!noIsBlank || !remarksIsBlank)) {
                    valType.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valType.hide();
                    DisableSave(false);
                    
                }

                if (noIsBlank && (!typeIsBlank || !remarksIsBlank)) {
                    valNo.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valNo.hide();
                    DisableSave(false);
                    
                }

            }


        }

        function ValidateReference(index)
        {
            var name = $.trim($('#txtReferenceName_' + index).val());
            var relationship = $.trim($('#txtReferenceRelationship_' + index).val());
            var contactNumbers = $.trim($('#txtReferenceContactNumbers_' + index).val());

            var valName = $('#valReferenceName_' + index);
            var valRelationship = $('#valReferenceRelationship_' + index);
            var valContactNumbers = $('#valReferenceContactNumbers_' + index);

            var nameIsBlank = name == '';
            var relIsBlank = relationship == '';
            var contactIsBlank = contactNumbers == '';

            if (nameIsBlank && relIsBlank && contactIsBlank) {
                
                valName.hide();
                valRelationship.hide();
                valContactNumbers.hide();
                DisableSave(false);
            }
            else {

                if (nameIsBlank && (!relIsBlank || !contactIsBlank)) {
                    valName.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valName.hide();
                    DisableSave(false);
                }

                if (relIsBlank && (!nameIsBlank || !contactIsBlank)) {
                    valRelationship.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valRelationship.hide();
                    DisableSave(false);
                }

                if (contactIsBlank && (!nameIsBlank || !relIsBlank)) {
                    valContactNumbers.show();
                    DisableSave(true);
                    return;
                }
                else {
                    valContactNumbers.hide();
                    DisableSave(false);
                }
            }
                
        }

        
    </script>
}
